package edu.ccut.saturn.studio.logic.contextaction;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.gef.ui.actions.SelectionAction;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.IWorkbenchPart;

import edu.ccut.saturn.studio.Activator;
import edu.ccut.saturn.studio.Images;
import edu.ccut.saturn.studio.core.util.eclipse.Messages;
import edu.ccut.saturn.studio.logic.model.Shape;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnActionNode;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnComponentNode;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnLogicNode;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnParameter;
import edu.ccut.saturn.studio.logic.model.saturn.logic.SaturnStartNodeInLogic;
import edu.ccut.saturn.studio.logic.parts.DiagramEditPart;
import edu.ccut.saturn.studio.logic.parts.ShapeEditPart;

public class Change extends SelectionAction {

	private Shape shape;
	List<Shape> shapes = new ArrayList<Shape>();
	public static final String ID = "change";
	private static final String SHOW_PARAMETER = Messages
			.getString("Change.ShowParameter"); //$NON-NLS-1$
	private static final String HIDE_PARAMETER = Messages
			.getString("Change.HideParameter"); //$NON-NLS-1$
	private Image descriptor = Activator.getImage(Images.PLUS);
	private Image fdescriptor = Activator.getImage(Images.REDUCE);
	public static  boolean label = false;

	public Change(IWorkbenchPart part) {
		super(part);
		setText(SHOW_PARAMETER);
		setImageDescriptor(ImageDescriptor.createFromImage(descriptor));
		setId(ID);
	}

	@SuppressWarnings("unchecked")
	protected boolean calculateEnabled() {
		shapes.clear();

		List<Object> objects = this.getSelectedObjects();
		if (objects.size() == 0) {
			super.update();
		}

		for (Object object : objects) {
			if (object instanceof ShapeEditPart) {
				ShapeEditPart ss = (ShapeEditPart) object;
				Shape shape = (Shape) ss.getModel();
				this.shapes.add(shape);
				if ((shape instanceof SaturnLogicNode)
						|| (shape instanceof SaturnComponentNode)
						|| (shape instanceof SaturnActionNode)
						|| (shape instanceof SaturnStartNodeInLogic)) {
					if (shape.getParameters().size() != 0) {
						if (shape.isIschanged() == true) {
							setText(HIDE_PARAMETER);
							setHoverImageDescriptor(ImageDescriptor
									.createFromImage(fdescriptor));
							label = true;
						} else {
							setText(SHOW_PARAMETER);
							setHoverImageDescriptor(ImageDescriptor
									.createFromImage(descriptor));
						}
						return true;
					}
				}
				return false;
			}
			if (object instanceof DiagramEditPart) {
				DiagramEditPart ss = (DiagramEditPart) object;
				List<ShapeEditPart> sourceShapes = ss.getChildren();
				for (int i = 1; i < sourceShapes.size(); i++) {
					Shape shape = (Shape) sourceShapes.get(i).getModel();
					if ((shape instanceof SaturnLogicNode)
							|| (shape instanceof SaturnComponentNode)
							|| (shape instanceof SaturnActionNode)
							|| (shape instanceof SaturnStartNodeInLogic)) {
						if (shape.getParameters().size() != 0) {
							try {
								if(label == true) {
									shape.refresh();
								}
							} catch (RuntimeException e) {
								e.printStackTrace();
							}
							shapes.add(shape);
						}
					}
				}
				if (shapes == null || shapes.size() == 0) {
					return false;
				}
				int count = 0;
				for (Shape shape : this.shapes) {
					if (shape.isIschanged() == true) {
						count++;
					}
				}
				if (count == shapes.size()) {
					setText(HIDE_PARAMETER);
					setHoverImageDescriptor(ImageDescriptor
							.createFromImage(fdescriptor));

				} else {
					setText(SHOW_PARAMETER);
					setHoverImageDescriptor(ImageDescriptor
							.createFromImage(descriptor));
				}
				return true;
			}
		}
		return true;
	}

	public void run() {

		if (shapes != null && shapes.size() != 0) {
			int count = 0;
			for (Shape shape : this.shapes) {
				if (shape.isIschanged() == false) {
					shape.setIschanged(true);
					setHoverImageDescriptor(ImageDescriptor
							.createFromImage(fdescriptor));
					this.shape = shape;
					shape.setSize(computersize(shape.getParameters()));
					++count;
				}

				if (shape.isIschanged() == true) {
					++count;
				}

			}
			if (count == shapes.size()) {
				for (Shape shape : this.shapes) {
					setText(SHOW_PARAMETER);
					shape.setIschanged(false);
					shape.setSize(new Dimension(54, 74));
					setHoverImageDescriptor(ImageDescriptor
							.createFromImage(descriptor));
				}
			} else {
				setText(HIDE_PARAMETER);
				shape.setIschanged(true);
				setHoverImageDescriptor(ImageDescriptor
						.createFromImage(fdescriptor));
				shape.setSize(computersize(shape.getParameters()));
			}

		}
	}

	private Dimension computersize(List<SaturnParameter> parameters)
	{
		int length = parameters.size();
		int x = 50, y = 70;

		x = (2 + this.shape.getName().length()) * 9;
		y = 50 + length * 14;
		Dimension size = new Dimension(x, y);
		return size;
	}

	public Image getDescriptor() {
		return descriptor;
	}

	public void setDescriptor(Image descriptor) {
		this.descriptor = descriptor;
	}
}
